<?php

namespace App\Http\Controllers\Admin\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Gprice;

class GoodsPriceResourceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 显示编辑页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Gprice::find($id)
            ->toArray();

        // dd($data);

        return view('Admin/Goods/product-detail-edit', ['goods' => $data, ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($sales);
        
        //验证表单
        $this->validate($request, [
            'size' => 'bail|required|min:1',
            'color' => 'bail|required|',
            'price' => 'bail|required|numeric|min:0|max:9999999.99',
            'storage' => 'bail|required|min:0|integer',
            'sales' => 'bail|required|min:0|integer',
        ],
        [
            'size.required' => '尺寸必填',
            'size.min' => '尺寸最少为1',
            'color.required' => '颜色必填',
            'price.required' => '价格必填',
            'price.numeric' => '价格应为数字',
            'price.min' => '价格最少为0',
            'price.max' => '价格最大为9999999.99',
            'stroage.required' => '库存必填',
            'stroage.min' => '库存最少为0',
            'stroage.integer' => '库存应为整数',
            'sales.required' => '销量必填',
            'sales.min' => '销量最少为0',
            'sales.integer' => '销量应为整数',

        ]);

        //执行更新
        $res = Gprice::where('id', $id)
            ->update([

                'size' => $request->input('size'),
                'color' => $request->input('color'),
                'price' => $request->input('price'),
                'storage' => $request->input('storage'),
                'sales' => $request->input('sales'),

            ]);

        if ($res) {
            
            // return back()->with('msg', '修改成功');
            return redirect('Admin/goods/'. $request->input('gid') )->with('msg', '修改成功');

        } else {
            // return back()->with('msg', '修改失败');
            return redirect('Admin/goods/'. $request->input('gid') )->with('msg', '修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
